package org.ddd.productdemo.domaindriven.application.service;

import org.ddd.productdemo.domaindriven.application.dto.OrderRequest;
import org.ddd.productdemo.domaindriven.application.dto.OrderResponse;
import org.ddd.productdemo.domaindriven.application.port.incoming.OrderApplicationService;
import org.ddd.productdemo.domaindriven.domain.model.aggregate.Order;
import org.ddd.productdemo.domaindriven.domain.model.aggregate.OrderId;
import org.ddd.productdemo.domaindriven.domain.model.entity.CustomerId;
import org.ddd.productdemo.domaindriven.domain.model.entity.Product;
import org.ddd.productdemo.domaindriven.domain.model.entity.ProductId;
import org.ddd.productdemo.domaindriven.domain.repository.OrderRepository;
import org.ddd.productdemo.domaindriven.domain.repository.ProductRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class OrderApplicationServiceImpl implements OrderApplicationService {

    private final OrderRepository orderRepository;
    private final ProductRepository productRepository;

    public OrderApplicationServiceImpl(OrderRepository orderRepository, ProductRepository productRepository) {
        this.orderRepository = orderRepository;
        this.productRepository = productRepository;
    }

    @Override
    @Transactional
    public OrderResponse createOrder(OrderRequest request) {
        // 创建订单领域对象
        Order order = new Order(new CustomerId(request.getCustomerId()));

        // 添加产品
        for (OrderRequest.OrderItem item : request.getItems()) {
            Product product = productRepository.findById(new ProductId(item.getProductId()))
                    .orElseThrow(() -> new RuntimeException("Product not found"));

            order.addProduct(product, item.getQuantity());

            // 减少库存
            product.decreaseStock(item.getQuantity());
            productRepository.save(product);
        }

        // 保存订单
        Order savedOrder = orderRepository.save(order);

        // 返回DTO
        return mapToDto(savedOrder);
    }

    @Override
    public Optional<OrderResponse> getOrder(String orderId) {
        return orderRepository.findById(new OrderId(orderId))
                .map(this::mapToDto);
    }

    @Override
    public List<OrderResponse> getCustomerOrders(String customerId) {
        return orderRepository.findByCustomerId(new CustomerId(customerId)).stream()
                .map(this::mapToDto)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void confirmOrder(String orderId) {
        Order order = orderRepository.findById(new OrderId(orderId))
                .orElseThrow(() -> new RuntimeException("Order not found"));

        order.confirm();
        orderRepository.save(order);
    }

    private OrderResponse mapToDto(Order order) {
        // 映射逻辑
        OrderResponse orderResponse = new OrderResponse();

        return orderResponse;
    }
}
